﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SeguradoraIA.Framework.EnumHelper
{
    /// <summary>
    /// Provide access to enum helpers
    /// </summary>
    public static class Enum<T>
    {
        private static readonly Type enumType;
        private static readonly DescribedEnumHandler<T> handler;

        static Enum()
        {
            enumType = typeof(T);
            if (enumType.IsEnum == false)
                throw new ArgumentException(string.Format("{0} is not an enum", enumType));

            handler = new DescribedEnumHandler<T>();
        }

        public static IDictionary<T, string> GetDescriptions()
        {
            return handler.GetDescriptions();
        }

        /// <summary>
        /// Extract the description for a given enum value
        /// </summary>
        /// <param name="value">An enum value</param>
        /// <returns>It's description, or it's name if there's no registered description for the given value</returns>
        public static string GetDescriptionOf(T value)
        {
            return handler.GetDescriptionFrom(value);
        }

        /// <summary>
        /// Gets the enum value for a given description or value
        /// </summary>
        /// <param name="descriptionOrName">The enum value or description</param>
        /// <returns>An enum value matching the given string value, as description (using <see cref="DescriptionAttribute"/>) or as value</returns>
        public static T FromDescription(string descriptionOrName)
        {
            return handler.GetValueFromDescription(descriptionOrName);
        }

        /// <summary>
        /// Extract the code for a given enum value
        /// </summary>
        /// <param name="value">An enum value</param>
        /// <returns>It's code, or it's name if there's no registered code for the given value</returns>
        public static string GetCodeOf(T value)
        {
            return handler.GetCodeFrom(value);
        }

        /// <summary>
        /// Gets the enum value for a given code or value
        /// </summary>
        /// <param name="codeOrName">The enum value or description</param>
        /// <returns>An enum value matching the given string value, as description (using <see cref="CodeAttribute"/>) or as value</returns>
        public static T FromCode(string codeOrName)
        {
            return handler.GetValueFromCode(codeOrName);
        }
    }
}